﻿using System.Linq;
using System.Web.Mvc;
using Century.Web.UI.MVC;
using Century.Business.CMS;

namespace Century.Admin.Controllers
{
    public class SiteSelectorController : CenturyAdminBaseController
    {
        public SiteSelectorController(ISiteService siteService) : base(siteService) { }
        public ActionResult Index()
        {
            if (System.Web.HttpContext.Current.Session["CurrentSiteId"] != null && Token.CurrentSite.Id != (int)System.Web.HttpContext.Current.Session["CurrentSiteId"])
            {
                Token.CurrentSite = siteService.GetSites().First(c => c.Id == (int)System.Web.HttpContext.Current.Session["CurrentSiteId"]);
            }
            return PartialView(siteService.GetSites());
        }

        public string CountrySiteChange(int? selectedCountrySiteId)
        {
            // set current site
            if (selectedCountrySiteId.HasValue)
            {
                Token.CurrentSite = siteService.GetSites().First(c => c.Id == selectedCountrySiteId);
                System.Web.HttpContext.Current.Session["CurrentSiteId"] = Token.CurrentSite.Id;
            }
            else
            {
                Token.CurrentSite = siteService.GetDefaulSite();
            }
            //Response.Redirect("/Admin");
            
            return Token.CurrentSite.Path;
        }
    }
}
